<?php

namespace App\Admin\Controllers;

use App\Models\Comment;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CommentController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '评论管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Comment());

        $grid->column('id', __('table.comment.id'));
        $grid->column('uid', __('table.comment.uid'));
        $grid->column('app', __('table.comment.app'));
        $grid->column('app_id', __('table.comment.app_id'));
        $grid->column('content', __('table.comment.content'));
        $grid->column('floor', __('table.comment.floor'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Comment::findOrFail($id));

        $show->field('id', __('table.comment.id'));
        $show->field('uid', __('table.comment.uid'));
        $show->field('app', __('table.comment.app'));
        $show->field('app_id', __('table.comment.app_id'));
        $show->field('content', __('table.comment.content'));
        $show->field('floor', __('table.comment.floor'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Comment());

        $form->select('uid', __('table.comment.uid'))->options(function($id){
            $user=User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/api/user');
        $form->text('app', __('table.comment.app'));
        $form->number('app_id', __('table.comment.app_id'));
        $form->text('content', __('table.comment.content'));
        $form->number('floor', __('table.comment.floor'));

        return $form;
    }
}
